package com.borun.dictionary;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.borun.bean.User;
import com.borun.dao.UserDao;

public class Regist extends JFrame implements MouseListener, ActionListener{

	private static final long serialVersionUID = 1L;
	
	private JLabel lblUserName,lblPassWord,lblToLogin,lblTitle;
	private JTextField txtUserName;
	private JPasswordField txtPassWord;
	private JButton btnRegist,btnReSet;
	
	public Regist(){
		
		super("注册窗体");
		this.setLayout(null);
		
		
		lblTitle = new JLabel("欢迎注册背单词系统");
		lblTitle.setFont(new Font("",Font.BOLD,24));
		lblTitle.setBounds(80, 10, 300, 30);
		
		lblUserName = new JLabel("帐户：");
		lblUserName.setBounds(60,70,70,30);
		
		txtUserName = new JTextField();
		txtUserName.setBounds(130,70,200,30);
		
		
		lblPassWord = new JLabel("密码：");
		lblPassWord.setBounds(60,110,70,30);
				
		txtPassWord = new JPasswordField();
		txtPassWord.setBounds(130,110,200,30);
		
		
		
		btnRegist = new JButton("注册");
		btnRegist.addActionListener(this);
		btnRegist.setBounds(90, 170, 80, 30);
		
		btnReSet = new JButton("重置");
		btnReSet.addActionListener(this);
		btnReSet.setBounds(210, 170, 80, 30);
		
		
		lblToLogin = new JLabel("已经有账号？点击去登录");
		lblToLogin.setForeground(Color.BLUE);
		lblToLogin.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		lblToLogin.addMouseListener(this);
		lblToLogin.setBounds(240, 230, 200, 20);
		
		this.add(lblTitle);
		this.add(lblUserName);
		this.add(txtUserName);
		this.add(lblPassWord);
		this.add(txtPassWord);
		this.add(btnRegist);
		this.add(btnReSet);
		this.add(lblToLogin);
		
		this.setSize(400,300);
		this.setLocationRelativeTo(null); // 设置窗口相对于指定组件的位置
		this.setResizable(false); // 禁止改变窗体大小
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
//	public static void main(String[] args) {
//		new Regist();
//	}

	@Override
	public void mouseClicked(MouseEvent e) {
		this.setVisible(false);
		new Login();
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == btnRegist) {
			String username = txtUserName.getText();
			if ("".equals(username) || username.trim().isEmpty()) {

				JOptionPane.showMessageDialog(null, "请正确填写用户名！");
				return;
			}
			String password = String.valueOf(txtPassWord.getPassword());
			if ("".equals(password) || password.trim().isEmpty()) {
				JOptionPane.showMessageDialog(null, "请正确填写密码！");
				return;
			}

			User user = new User(username, password);
			try {
				if (UserDao.Regist(user)) {
					JOptionPane.showMessageDialog(null, "注册成功！即将为您跳转到登录界面。");
					this.setVisible(false);
					new Login();
				} else {				
					JOptionPane.showMessageDialog(null, "系统异常，注册失败！");
				}
			} catch (HeadlessException e1) {
				e1.printStackTrace();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == btnReSet) {
			txtUserName.setText(null);
			txtPassWord.setText(null);
		}
	}

}
